Practical-12
Mobile Application
Practical List
Create an android program to send a message between two emulators and read messages from any one emulator (mobile).
Steps
- Create a new Android project in Android Studio.
- Open the
activity_main.xmllayout file. - Add two
EditTextelements to the layout with the following attributes:-
android:id="@+id/editTextSender" -
android:layout_width="match_parent" -
android:layout_height="wrap_content" -
android:hint="Enter message to send" -
android:inputType="text" -
android:layout_marginTop="16dp" -
android:layout_marginBottom="16dp" -
android:layout_marginStart="16dp" -
android:layout_marginEnd="16dp" -
android:imeOptions="actionSend" -
android:maxLines="1" -
android:singleLine="true" -
android:textSize="16sp" -
android:textColor="@android:color/black" -
android:id="@+id/editTextReceiver" -
android:layout_width="match_parent" -
android:layout_height="wrap_content" -
android:hint="Received message will be displayed here" -
android:inputType="text" -
android:layout_marginTop="16dp" -
android:layout_marginBottom="16dp" -
android:layout_marginStart="16dp" -
android:layout_marginEnd="16dp" -
android:imeOptions="actionDone" -
android:maxLines="1" -
android:singleLine="true" -
android:textSize="16sp" -
android:textColor="@android:color/black" -
android:enabled="false"
-
- Open the
MainActivity.javafile. - Inside the
onCreatemethod, add the following code to set the content view to theactivity_main.xmllayout:setContentView(R.layout.activity_main); - Create a new method called
sendMessageto send a message between two emulators:private void sendMessage() {
EditText editTextSender = findViewById(R.id.editTextSender);
EditText editTextReceiver = findViewById(R.id.editTextReceiver);
String message = editTextSender.getText().toString();
editTextReceiver.setText(message);
} - Inside the
onCreatemethod, add the following code to set theOnClickListenerfor the send button:Button sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
}); - Run the application on two emulators or devices.
- Enter a message in the sender emulator/device and click the send button.
- The message should be displayed in the receiver emulator/device.